package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageData;
import com.woniu.entity.dto.ExperimentDTO;
import com.woniu.entity.vo.ExperimentVO;
import com.woniu.mapper.ExperimentMapper;
import com.woniu.pojo.Comment;
import com.woniu.pojo.Experiment;
import com.woniu.service.IExperimentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaosong
 * @since 2023-04-03
 */
@Service
public class ExperimentServiceImpl extends ServiceImpl<ExperimentMapper, Experiment> implements IExperimentService {
//    分页查询
    @Autowired(required = false)
    private ExperimentMapper experimentMapper;

    @Override
    public PageData search(Integer currentPage, Integer pageSize, ExperimentDTO experimentDTO) {

//        开启分页
        IPage<ExperimentVO> page = new Page<>(currentPage,pageSize);
        List<ExperimentVO> experimentVOS = experimentMapper.selectall(experimentDTO);

        IPage<ExperimentVO> page1 = page.setRecords(experimentVOS);

//         封装PageData对象
        PageData pageData = new PageData(page1.getTotal(),page1.getRecords());

        return pageData;

    }

    @Override
    public List<Experiment> selectByid(Integer id) {
        QueryWrapper<Experiment> wrapper = new QueryWrapper<>();
         wrapper.eq("id",id);
         List<Experiment> experimentList = experimentMapper.selectList(wrapper);
        return experimentList;
    }

    @Override
    public Integer updateExperment(Experiment experiment) {
        return experimentMapper.updateById(experiment);
    }
    @Override
    public boolean saveExperiment(Experiment experiment) {
        QueryWrapper<Experiment> wrapper = new QueryWrapper<>();
        wrapper.eq("training_id",experiment.getTrainingId());
        Experiment selectOne = experimentMapper.selectOne(wrapper);
        if (selectOne !=null){
            return false;
        }else {
            experimentMapper.insert(experiment);
            return  true;
        }


    }

    @Override
    public Experiment findByid(Integer id) {
        QueryWrapper<Experiment> wrapper = new QueryWrapper<>();
        wrapper.eq("training_id",id);
        Experiment experimentList = experimentMapper.selectOne(wrapper);
        return experimentList;

    }
}
